package com.wc.alorithm_blue_bridge._搜索与图论.扫雷;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * @Author congge
 * @Date 2023/10/23 22:48
 * @description 扫雷
 * https://www.lanqiao.cn/problems/549/learning/?page=3&first_category_id=1&sort=students_count&second_category_id=3
 */
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        Input sc = new Input();

        int m = sc.nextInt();
        int n = sc.nextInt();

        int[][] map = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = sc.nextInt();
                if (map[i][j] == 1) {
                    map[i][j] = 9;
                }
            }
        }
        StringBuffer ans = new StringBuffer();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                main.paint(map, i, j, 9);
                if (j != 0) {
                    ans.append(' ');
                }
                ans.append(map[i][j]);
            }
            ans.append('\n');
        }
        System.out.println(ans);
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }

    public void paint(int[][] map, int i, int j, int light) {
        if (map[i][j] == light) {
            return;
        }
        int m = map.length;
        int n = map[0].length;
        int up = i - 1;
        int down = i + 1;
        int left = j - 1;
        int right = j + 1;
        int ans = 0;
        /**
         * 正方形遍历
         */
        for (int k = up; k <= down; k++) {
            for (int l = left; l <= right; l++) {
                if (isNotBorder(k, l, m, n) && map[k][l] == light) {
                    ans++;
                }
            }
        }
        map[i][j] = ans;
    }

    public boolean isNotBorder(int i, int j, int m, int n) {
        return i >= 0 && i < m && j >= 0 && j < n;
    }
}
